package nsh.cl.nd.khtn.mazedash.game.play;

import nsh.cl.nd.khtn.mazedash.GameConstant;
import nsh.cl.nd.khtn.mazedash.PrefService;
import nsh.cl.nd.khtn.mazedash.RootGameActivity;

import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.FadeInModifier;
import org.andengine.entity.modifier.FadeOutModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.ButtonSprite;
import org.andengine.entity.sprite.ButtonSprite.OnClickListener;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.util.HorizontalAlign;
import org.andengine.util.color.Color;
import org.andengine.util.modifier.IModifier;
import org.andengine.util.modifier.ease.EaseBackOut;
import org.andengine.util.modifier.ease.EaseElasticOut;
import org.andengine.util.modifier.ease.EaseQuartIn;

import android.content.Intent;
import android.net.Uri;

public class GameOverPopupScene extends Scene implements OnClickListener, IEntityModifierListener {
	private BitmapTextureAtlas backgroundTextureAtlas;
	private BitmapTextureAtlas buttonTextureAtlas;

	private TextureRegion fadeTextureRegion;
	private TextureRegion boxTextureRegion;
	private Text message;

	private Text scoreLabel;
	private Text scoreValue;
	private Text bestLabel;
	private Text bestValue;

	private TiledTextureRegion buttonReplayTextureRegion;
	private TiledTextureRegion buttonMainMenuTextureRegion;
	private TiledTextureRegion buttonRateTextureRegion;

	private ButtonSprite buttonReplay;
	private ButtonSprite buttonMainMenu;
	private ButtonSprite buttonRate;
	private Sprite fadeBackground;
	private Sprite boxBackground;

	private PlayScene parent;

	public void loadResource() {
		this.setBackgroundEnabled(false);
		this.setTouchAreaBindingOnActionDownEnabled(true);
		this.setTouchAreaBindingOnActionMoveEnabled(true);

		buttonTextureAtlas = new BitmapTextureAtlas(RootGameActivity.getInstance().getTextureManager(),
				GameConstant.TEXTUREATLAS_MAX_WIDTH, GameConstant.TEXTUREATLAS_MAX_HEIGHT);
		backgroundTextureAtlas = new BitmapTextureAtlas(RootGameActivity.getInstance().getTextureManager(),
				GameConstant.TEXTUREATLAS_MAX_WIDTH, GameConstant.TEXTUREATLAS_MAX_HEIGHT);

		fadeTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(backgroundTextureAtlas,
				RootGameActivity.getInstance(), GameConstant.PATH_MASK_FADE, 0, 0);
		boxTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(backgroundTextureAtlas,
				RootGameActivity.getInstance(), "boxmenu.png", 1024, 0);
		buttonReplayTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(buttonTextureAtlas,
				RootGameActivity.getInstance(), "button/replay_one.png", 512, 0, 2, 1);
		buttonMainMenuTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(buttonTextureAtlas,
				RootGameActivity.getInstance(), "button/menu.png", 1024, 0, 2, 1);
		buttonRateTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(buttonTextureAtlas,
				RootGameActivity.getInstance(), "button/rate.png", 0, 512, 2, 1);
		message = new Text(0, 0, RootGameActivity.getInstance().getScreenManager().getGameFont(), "GAME OVER", 100,
				new TextOptions(HorizontalAlign.CENTER), RootGameActivity.getInstance().getVertexBufferObjectManager());

		scoreLabel = new Text(0, 0, RootGameActivity.getInstance().getScreenManager().getSmallFont(), "score", 100,
				new TextOptions(HorizontalAlign.CENTER), RootGameActivity.getInstance().getVertexBufferObjectManager());
		scoreValue = new Text(0, 0, RootGameActivity.getInstance().getScreenManager().getSmallFont(), "10", 100,
				new TextOptions(HorizontalAlign.CENTER), RootGameActivity.getInstance().getVertexBufferObjectManager());
		bestLabel = new Text(0, 0, RootGameActivity.getInstance().getScreenManager().getSmallFont(), "best", 100,
				new TextOptions(HorizontalAlign.CENTER), RootGameActivity.getInstance().getVertexBufferObjectManager());
		bestValue = new Text(0, 0, RootGameActivity.getInstance().getScreenManager().getSmallFont(), "999999", 100,
				new TextOptions(HorizontalAlign.CENTER), RootGameActivity.getInstance().getVertexBufferObjectManager());

		message.setPosition(GameConstant.CAMERA_WIDTH / 2 - message.getWidth() / 2, GameConstant.CAMERA_HEIGHT / 2
				- message.getHeight() / 2 - 90);
		message.setScale(0.9f);

		buttonTextureAtlas.load();
		backgroundTextureAtlas.load();

		final float centerX = GameConstant.CAMERA_WIDTH / 2;
		final float centerY = GameConstant.CAMERA_HEIGHT / 2;

		fadeBackground = new Sprite(0, 0, fadeTextureRegion, RootGameActivity.getInstance()
				.getVertexBufferObjectManager());
		boxBackground = new Sprite(centerX - boxTextureRegion.getWidth() / 2, centerY - boxTextureRegion.getHeight()
				/ 2, boxTextureRegion, RootGameActivity.getInstance().getVertexBufferObjectManager());

		buttonReplay = new ButtonSprite(centerX - buttonReplayTextureRegion.getWidth() - 100,
				GameConstant.CAMERA_HEIGHT / 2 + 100, buttonReplayTextureRegion.getTextureRegion(0),
				buttonReplayTextureRegion.getTextureRegion(1), RootGameActivity.getInstance()
						.getVertexBufferObjectManager(), this);

		buttonMainMenu = new ButtonSprite(centerX - buttonRateTextureRegion.getWidth() / 2,
				GameConstant.CAMERA_HEIGHT / 2 + 100, buttonMainMenuTextureRegion.getTextureRegion(0),
				buttonMainMenuTextureRegion.getTextureRegion(1), RootGameActivity.getInstance()
						.getVertexBufferObjectManager(), this);

		buttonRate = new ButtonSprite(centerX + 100, GameConstant.CAMERA_HEIGHT / 2 + 100,
				buttonRateTextureRegion.getTextureRegion(0), buttonRateTextureRegion.getTextureRegion(1),
				RootGameActivity.getInstance().getVertexBufferObjectManager(), this);

		attachChild(fadeBackground);
		attachChild(boxBackground);
		attachChild(buttonReplay);
		attachChild(buttonMainMenu);
		attachChild(buttonRate);
		attachChild(message);

		attachChild(scoreLabel);
		attachChild(scoreValue);
		attachChild(bestLabel);
		attachChild(bestValue);
	}

	public void animateIn() {
		fadeBackground.clearEntityModifiers();
		boxBackground.clearEntityModifiers();
		buttonReplay.clearEntityModifiers();
		buttonMainMenu.clearEntityModifiers();
		buttonRate.clearEntityModifiers();
		message.clearEntityModifiers();
		scoreLabel.clearEntityModifiers();
		scoreValue.clearEntityModifiers();
		bestLabel.clearEntityModifiers();
		bestValue.clearEntityModifiers();

		fadeBackground.setAlpha(0.0f);
		boxBackground.setScale(0.0f);
		buttonReplay.setScale(0.0f);
		buttonMainMenu.setScale(0.0f);
		buttonRate.setScale(0.0f);
		message.setScale(0.0f);
		scoreLabel.setAlpha(0.0f);
		scoreValue.setAlpha(0.0f);
		bestLabel.setAlpha(0.0f);
		bestValue.setAlpha(0.0f);

		fadeBackground.registerEntityModifier(new FadeInModifier(0.5f, new IEntityModifierListener() {
			@Override
			public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {
			}

			@Override
			public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
				buttonReplay.setEnabled(true);
				buttonMainMenu.setEnabled(true);
				buttonRate.setEnabled(true);
				onReady();
			}
		}));
		boxBackground.registerEntityModifier(new ScaleModifier(0.5f, 0.1f, 1.0f, EaseBackOut.getInstance()));
		buttonReplay.registerEntityModifier(new ScaleModifier(0.5f, 0.1f, 1.0f, EaseBackOut.getInstance()));
		buttonMainMenu.registerEntityModifier(new ScaleModifier(0.5f, 0.1f, 1.0f, EaseBackOut.getInstance()));
		buttonRate.registerEntityModifier(new ScaleModifier(0.5f, 0.1f, 1.0f, EaseBackOut.getInstance()));
		message.registerEntityModifier(new ScaleModifier(1.0f, 0.1f, 0.9f, EaseElasticOut.getInstance()));
		scoreLabel.registerEntityModifier(new FadeInModifier(0.5f));
		scoreValue.registerEntityModifier(new FadeInModifier(0.5f));
		bestLabel.registerEntityModifier(new FadeInModifier(0.5f));
		bestValue.registerEntityModifier(new FadeInModifier(0.5f));
	}

	private static final int REQUEST_LEADERBOARD = 0;

	protected void onReady() {
//		RootGameActivity instance = RootGameActivity.getInstance();
//		GameHelper gameServiceHelper = instance.getGameServiceHelper();
//		GameMode gameMode = parent.getGameMode();
//		if (gameServiceHelper.isSignedIn()) {
//			Games.Leaderboards.submitScore(
//					gameServiceHelper.getApiClient(),
//					instance.getResources().getString(
//							gameMode == GameMode.ONEWAY ? R.string.leaderboard_easy : R.string.leaderboard_hard),
//					PrefService.getBestScore(gameMode));
//		}
	}

	public void animateOut(IEntityModifierListener afterAnimation) {
		fadeBackground.clearEntityModifiers();
		boxBackground.clearEntityModifiers();
		buttonReplay.clearEntityModifiers();
		buttonMainMenu.clearEntityModifiers();
		buttonRate.clearEntityModifiers();
		message.clearEntityModifiers();

		fadeBackground.setAlpha(1.0f);
		boxBackground.setScale(1.0f);
		buttonReplay.setScale(1.0f);
		buttonMainMenu.setScale(1.0f);
		buttonRate.setScale(1.0f);
		message.setScale(0.9f);

		fadeBackground.registerEntityModifier(new FadeOutModifier(0.5f, afterAnimation));
		boxBackground.registerEntityModifier(new ScaleModifier(0.5f, 1.0f, 0.0f, EaseQuartIn.getInstance()));
		buttonReplay.registerEntityModifier(new ScaleModifier(0.5f, 1.0f, 0.0f, EaseQuartIn.getInstance()));
		buttonMainMenu.registerEntityModifier(new ScaleModifier(0.5f, 1.0f, 0.0f, EaseQuartIn.getInstance()));
		buttonRate.registerEntityModifier(new ScaleModifier(0.5f, 1.0f, 0.0f, EaseQuartIn.getInstance()));
		message.registerEntityModifier(new ScaleModifier(0.5f, 0.9f, 0.0f, EaseQuartIn.getInstance()));
	}

	public boolean showPopup(PlayScene parent) {
		this.unregisterTouchArea(buttonReplay);
		this.unregisterTouchArea(buttonMainMenu);
		this.unregisterTouchArea(buttonRate);

		this.parent = parent;
		buttonReplay.setEnabled(false);
		buttonMainMenu.setEnabled(false);
		buttonRate.setEnabled(false);

		this.registerTouchArea(buttonReplay);
		this.registerTouchArea(buttonMainMenu);
		this.registerTouchArea(buttonRate);

		GameMode gameMode = GameMode.NORMAL;
		bestValue.setText(PrefService.getBestScore(gameMode) + "");
		//TODO AHO get score and display here
		scoreValue.setText(00000 + "");
		scoreLabel.setPosition(GameConstant.CAMERA_WIDTH / 2 - scoreLabel.getWidth() / 2 - 70,
				GameConstant.CAMERA_HEIGHT / 2 - 40);
		scoreValue.setPosition(GameConstant.CAMERA_WIDTH / 2 - scoreValue.getWidth() / 2 + 110,
				GameConstant.CAMERA_HEIGHT / 2 - 40);
		bestLabel.setPosition(GameConstant.CAMERA_WIDTH / 2 - bestLabel.getWidth() / 2 - 70,
				GameConstant.CAMERA_HEIGHT / 2 + 20);
		bestValue.setPosition(GameConstant.CAMERA_WIDTH / 2 - bestValue.getWidth() / 2 + 110,
				GameConstant.CAMERA_HEIGHT / 2 + 20);

		Color color = Color.YELLOW;
		scoreLabel.setColor(color);
		scoreValue.setColor(color);
		bestLabel.setColor(color);
		bestValue.setColor(color);

		animateIn();
		parent.setChildScene(this, false, true, true);
		return true;
	}

	private void hidePopup(Scene parent, IEntityModifierListener afterAnimation) {
		reset();
		back();
	}

	public void hidePopup() {
		hidePopup(parent, GameOverPopupScene.this);
	}

	@Override
	public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
		if (pButtonSprite == this.buttonReplay) {
			this.unregisterTouchArea(buttonReplay);
			this.unregisterTouchArea(buttonMainMenu);
			this.unregisterTouchArea(buttonRate);
			hidePopup(parent, GameOverPopupScene.this);
			parent.startNewGame();
		} else if (pButtonSprite == this.buttonMainMenu) {
			
		} else if (pButtonSprite == this.buttonRate) {
			Intent browserIntent = new Intent(Intent.ACTION_VIEW,
					Uri.parse("https://play.google.com/store/apps/details?id=nsh.cl.nd.khtn.filltheframe"));
			RootGameActivity.getInstance().startActivity(browserIntent);
		}
	}

	@Override
	public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {
	}

	@Override
	public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
		if (pModifier.getClass() == FadeOutModifier.class) {
			back();
			reset();
		}
	}

}
